/**
 * Created by jiangtao on 2017/5/17.
 */
const path = require('path');
let fs = require('fs');
let process = require('process');
let webpack = require('webpack');

let HtmlWebpackPlugin = require('html-webpack-plugin');

let ExtractTextPlugin = require("extract-text-webpack-plugin");

//环境变量
const node_env = process.env.NODE_ENV;

//页面
let pages = require("./webpack.page.config");

//如果是正式环境的打包,那么就在css、js、图片、字体文件后面加上hash
const Hash8 = node_env !== 'dev' ? '-[chunkhash]' : '';

//基本配置
let webpackBaseConfig = {
    // 入口
    entry: {
        common: [
            'react',
            'react-dom',
            'react-router',
            'react-router-dom',
            path.join(__dirname, "src/common/common.js")
        ],
    },
    // 输出
    output: {
        path: path.join(__dirname, "/dist"),
        publicPath: "",
        filename: "js/[name]" + Hash8 + ".js",
        chunkFilename: "js/[name]" + Hash8 + ".js"
    },
    // 加载器
    module: {
        rules: [
            {
                test: /\.js?$/,
                use: 'babel-loader',
                exclude: path.resolve(__dirname, 'node_modules'),
                include: path.resolve(__dirname, 'src'),//优化babel 打包范围
            },
            //加载css、scss文件
            {
                test: /\.(css|scss)$/,
                use: (() => {
                    let loader = [
                        'style-loader',
                        {
                            loader: 'css-loader',
                            options: {
                                modules: true,
                                localIdentName: "[local]-[hash:base64:16]",
                                camelCase: true,
                            }
                        },
                        'postcss-loader',
                        'sass-loader'
                    ];
                    return node_env === 'dev' ? ["css-hot-loader"].concat(loader) : loader
                })()
            },
            //加载图片
            {
                test: /\.(png|jpg|svg|gif)$/,
                use: "url-loader?limit=20480&name=images/[name].[ext]"
            },
            {
                test: /\.jpeg?$/,
                use: "file-loader?limit=20480&name=images/[name].[ext]"
            },
            //加载字体
            {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: "url-loader?limit=10000&mimetype=application/font-woff&name=fonts/[name].[ext]"
            },
            {
                test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: "file-loader?name=fonts/[name].[ext]"
            }
        ],

    },
    resolve: {
        // require时省略的扩展名，如：require('module') 不需要module.js
        extensions: ['.js', '.jsx'],
        // 别名，可以直接使用别名来代表设定的路径以及其他
        alias: {
            'src': path.resolve(__dirname, '/src'),
            'img': path.resolve(__dirname, '/src/images'),
            'components': path.resolve(__dirname, '/src/components')
        }
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: "common",
            filename: "js/common" + Hash8 + ".js"
        }),
        new webpack.ProvidePlugin({
            $:'jquery'
        })
        //提取css,将它放在css文件夹下
        // new ExtractTextPlugin({
        //     allChunks: true,
        //     filename: "css/[name]" + Hash8 + ".css"
        // })
    ]
};

pages.forEach(function (item) {
    //将入口添加到entry中
    webpackBaseConfig.entry[item.chunk] = item.viewPath + item.chunk + '.js';
    webpackBaseConfig.plugins.push(new HtmlWebpackPlugin({
        title: "",
        filename: item.filename + ".html",
        template: "template/tpl.html",
        chunks: ["manifest", "common", item.chunk],
        inject: false,
    }));
});


module.exports = webpackBaseConfig;